package com.relytosoft.mizar.media.alarm.api.entity;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.relytosoft.dubhe.common.core.base.BaseEntity;
import com.relytosoft.mizar.media.common.entity.algo.AYVehicle;
import com.relytosoft.mizar.media.common.entity.algo.AYVideoSliceCommon;
import com.relytosoft.mizar.media.common.entity.pojo.dto.AyDispositionPlanDto;
import com.relytosoft.mizar.media.common.entity.pojo.request.btdl.common.BtdlBehaviorDataReq;
import com.relytosoft.mizar.media.common.entity.pojo.request.btdl.common.BtdlFaceControlReq;
import com.relytosoft.mizar.media.common.entity.pojo.request.btdl.common.BtdlStructureControlReq;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Map;

import static com.relytosoft.mizar.media.common.constant.AlarmTypeConstants.COVER_VEHICLE_ALARM;

/**
 * @author wss
 * @version 1.0
 * @date 2022/5/24 11:47
 */
@TableName("VID_ALARM")
@ApiModel("Alarm")
public class Alarm extends BaseEntity {

    private static final long serialVersionUID = 1L;


    @ApiModelProperty(value = "id", required = true)
    @TableField("ID")
    @NotNull(message = "不能为空")
    private Integer id;

    @ApiModelProperty(value = "报警类型id")
    @TableField("ALARM_TYPE_ID")
    private Integer alarmTypeId;

    @ApiModelProperty(value = "设备id")
    @TableField("CAMERA_ID")
    private String cameraId;

    @ApiModelProperty(value = "设备编码")
    @TableField("CAMERA_CODE")
    private String cameraCode;

    @ApiModelProperty(value = "设备名称")
    @TableField("CAMERA_NAME")
    private String cameraName;

    @ApiModelProperty(value = "任务id")
    @TableField("TASK_ID")
    private String taskId;

    @ApiModelProperty(value = "任务名称")
    @TableField("TASK_NAME")
    private String taskName;

    @ApiModelProperty(value = "报警图片")
    @TableField("ALARM_IMG")
    private String alarmImg;

    @ApiModelProperty(value = "报警video")
    @TableField("ALARM_VIDEO")
    private String alarmVideo;

    @ApiModelProperty(value = "报警时间")
    @TableField("ALARM_TIME")
    private LocalDateTime alarmTime;

    @ApiModelProperty(value = "备注")
    @TableField("REMARK")
    private String remark;

    @ApiModelProperty(value = "报警数据")
    @TableField("ALARM_DATA")
    private String alarmData;


    @ApiModelProperty(value = "状态", required = true)
    @TableField("STATUS")
    @NotBlank(message = "不能为空")
    private Integer status;

    /**
     * 构建结构化报警对象
     *
     * @param dispositionPlanDto 布控计划dto
     * @param bigPic             大图
     * @param ayVideoSliceCommon 报警明细对象
     * @return
     */
    public static Alarm buildStructureAlarm(AyDispositionPlanDto dispositionPlanDto, Map<String, String> bigPic, AYVideoSliceCommon ayVideoSliceCommon) {
        for (String imageKey : bigPic.keySet()) {
            Alarm alarm = new Alarm();
            alarm.setAlarmTypeId(dispositionPlanDto.getAlarmTypeId());
            alarm.setTaskId(dispositionPlanDto.getDispositionId());
            alarm.setAlarmImg(bigPic.get(imageKey));
            alarm.setCameraId(dispositionPlanDto.getDeviceId());
            alarm.setAlarmTime(LocalDateTime.now());
            alarm.setCameraName(dispositionPlanDto.getCameraName());
            alarm.setCameraCode(dispositionPlanDto.getCameraCode());
            alarm.setTaskName(dispositionPlanDto.getTaskName());
            alarm.setRemark("结构化原始数据告警");
            alarm.setAlarmData(JSON.toJSONString(ayVideoSliceCommon));
            return alarm;
        }
        return null;
    }

    /**
     * 构建结构化布控告警对象
     *
     * @param structureAlarm     结构化报警实体
     * @param dispositionPlanDto 布控计划对象
     * @return
     */
    public static Alarm buildStructureControlAlarm(BtdlStructureControlReq.Alarm structureAlarm, AyDispositionPlanDto dispositionPlanDto) {
        Alarm alarm = new Alarm();
        alarm.setAlarmTypeId(dispositionPlanDto.getAlarmTypeId());
        alarm.setTaskId(dispositionPlanDto.getDispositionId());
        alarm.setAlarmImg(structureAlarm.getAlarmData().getImage());
        alarm.setCameraId(dispositionPlanDto.getDeviceId());
        alarm.setAlarmTime(LocalDateTimeUtil.of(structureAlarm.getRecordTime()));
        alarm.setCameraName(dispositionPlanDto.getCameraName());
        alarm.setCameraCode(dispositionPlanDto.getCameraCode());
        alarm.setTaskName(dispositionPlanDto.getTaskName());
        alarm.setRemark("结构化布控告警");
        alarm.setAlarmData(JSON.toJSONString(structureAlarm.getAlarmData()));
        return alarm;
    }

    /**
     * 构建遮挡车牌报警对象
     *
     * @param ayVehicle 车辆对象
     * @param bigPic    图片对象
     * @return
     */
    public static Alarm buildCoverVehicleAlarm(AYVehicle ayVehicle, Map<String, String> bigPic, String deviceId) {
        for (String imageKey : bigPic.keySet()) {
            Alarm alarm = new Alarm();
            alarm.setAlarmImg(bigPic.get(imageKey));
            alarm.setAlarmTypeId(COVER_VEHICLE_ALARM);
            alarm.setCameraId(deviceId);
            alarm.setAlarmTime(LocalDateTime.now());
            alarm.setRemark("结构化无牌车报警告警");
            alarm.setAlarmData(JSON.toJSONString(ayVehicle));
            return alarm;
        }
        return null;
    }

    /**
     * 构建人脸报警对象
     *
     * @param dispositionPlanDto 布控计划dto
     * @param bigPic             大图
     * @param ayVideoSliceCommon 报警明细对象
     * @return
     */
    public static Alarm buildFaceAlarm(AyDispositionPlanDto dispositionPlanDto, Map<String, String> bigPic, AYVideoSliceCommon ayVideoSliceCommon) {
        for (String imageKey : bigPic.keySet()) {
            Alarm alarm = new Alarm();
            alarm.setAlarmTypeId(dispositionPlanDto.getAlarmTypeId());
            alarm.setTaskId(dispositionPlanDto.getDispositionId());
            alarm.setAlarmImg(bigPic.get(imageKey));
            alarm.setCameraId(dispositionPlanDto.getDeviceId());
            alarm.setAlarmTime(LocalDateTime.now());
            alarm.setCameraName(dispositionPlanDto.getCameraName());
            alarm.setCameraCode(dispositionPlanDto.getCameraCode());
            alarm.setTaskName(dispositionPlanDto.getTaskName());
            alarm.setRemark("结构化原始数据告警");
            alarm.setAlarmData(JSON.toJSONString(ayVideoSliceCommon));
            return alarm;
        }
        return null;
    }

    /**
     * 构建人脸布控报警对象
     *
     * @param btdlFaceControlReq 比特大陆人脸请求对象
     * @param dispositionPlanDto 布控计划对象
     * @return
     */
    public static Alarm buildFaceControlAlarm(BtdlFaceControlReq btdlFaceControlReq, AyDispositionPlanDto dispositionPlanDto) {
        Alarm alarm = new Alarm();
        alarm.setAlarmTypeId(dispositionPlanDto.getAlarmTypeId());
        alarm.setTaskId(dispositionPlanDto.getDispositionId());
        alarm.setAlarmImg(btdlFaceControlReq.getgAFace().getImage());
        alarm.setCameraId(dispositionPlanDto.getDeviceId());
        alarm.setAlarmTime(LocalDateTimeUtil.of(btdlFaceControlReq.getgAFace().getMarkTime()));
        alarm.setCameraName(dispositionPlanDto.getCameraName());
        alarm.setCameraCode(dispositionPlanDto.getCameraCode());
        alarm.setTaskName(dispositionPlanDto.getTaskName());
        alarm.setRemark("人脸布控告警");
        alarm.setAlarmData(JSON.toJSONString(btdlFaceControlReq.getgAFace()));
        return alarm;
    }

    /**
     * 构建行为分析报警对象
     *
     * @param dispositionPlanDto  布控计划dto
     * @param btdlBehaviorDataReq 比特大陆行为分析数据请求对象
     * @return
     */
    public static Alarm buildBehaviorAlarm(AyDispositionPlanDto dispositionPlanDto, BtdlBehaviorDataReq btdlBehaviorDataReq) {
        Alarm alarm = new Alarm();
        alarm.setAlarmTypeId(dispositionPlanDto.getAlarmTypeId());
        alarm.setTaskId(dispositionPlanDto.getDispositionId());
        alarm.setAlarmImg(btdlBehaviorDataReq.getImageData());
        alarm.setCameraId(dispositionPlanDto.getDeviceId());
        alarm.setAlarmTime(LocalDateTimeUtil.of(btdlBehaviorDataReq.getMarkTime()));
        alarm.setCameraName(dispositionPlanDto.getCameraName());
        alarm.setCameraCode(dispositionPlanDto.getCameraCode());
        alarm.setTaskName(dispositionPlanDto.getTaskName());
        alarm.setRemark("行为分析原始数据告警");
        alarm.setAlarmData(JSON.toJSONString(btdlBehaviorDataReq));
        return alarm;
    }


    /**
     * 获得
     */
    public Integer getId() {
        return this.id;
    }

    /**
     * 设置
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * 获得
     */
    public Integer getAlarmTypeId() {
        return this.alarmTypeId;
    }

    /**
     * 设置
     */
    public void setAlarmTypeId(Integer alarmTypeId) {
        this.alarmTypeId = alarmTypeId;
    }

    /**
     * 获得
     */
    public String getCameraId() {
        return this.cameraId;
    }

    /**
     * 设置
     */
    public void setCameraId(String cameraId) {
        this.cameraId = cameraId;
    }

    /**
     * 获得
     */
    public String getCameraCode() {
        return this.cameraCode;
    }

    /**
     * 设置
     */
    public void setCameraCode(String cameraCode) {
        this.cameraCode = cameraCode;
    }

    /**
     * 获得
     */
    public String getCameraName() {
        return this.cameraName;
    }

    /**
     * 设置
     */
    public void setCameraName(String cameraName) {
        this.cameraName = cameraName;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    /**
     * 获得
     */
    public String getTaskName() {
        return this.taskName;
    }

    /**
     * 设置
     */
    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    /**
     * 获得
     */
    public String getAlarmImg() {
        return this.alarmImg;
    }

    /**
     * 设置
     */
    public void setAlarmImg(String alarmImg) {
        this.alarmImg = alarmImg;
    }

    /**
     * 获得
     */
    public String getAlarmVideo() {
        return this.alarmVideo;
    }

    /**
     * 设置
     */
    public void setAlarmVideo(String alarmVideo) {
        this.alarmVideo = alarmVideo;
    }

    public LocalDateTime getAlarmTime() {
        return alarmTime;
    }

    public void setAlarmTime(LocalDateTime alarmTime) {
        this.alarmTime = alarmTime;
    }

    /**
     * 获得
     */
    public String getRemark() {
        return this.remark;
    }

    /**
     * 设置
     */
    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getAlarmData() {
        return alarmData;
    }

    public void setAlarmData(String alarmData) {
        this.alarmData = alarmData;
    }

    /**
     * 获得
     */
    public Integer getStatus() {
        return this.status;
    }

    /**
     * 设置
     */
    public void setStatus(Integer status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Alarm{" +
                "id=" + id +
                ", alarmTypeId=" + alarmTypeId +
                ", cameraId='" + cameraId + '\'' +
                ", cameraCode='" + cameraCode + '\'' +
                ", cameraName='" + cameraName + '\'' +
                ", taskId='" + taskId + '\'' +
                ", taskName='" + taskName + '\'' +
                ", alarmImg='" + alarmImg + '\'' +
                ", alarmVideo='" + alarmVideo + '\'' +
                ", alarmTime=" + alarmTime +
                ", remark='" + remark + '\'' +
                ", alarmData='" + alarmData + '\'' +
                ", status=" + status +
                '}';
    }
}

